package com.chat.datea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.chat.datea.R;
import com.chat.datea.bean.SearchSongEntity;

import java.util.ArrayList;

/**
 * Created by 周橹 on 15/10/29.
 */
public class SearchAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<SearchSongEntity> mEntities;

    public SearchAdapter(Context context, ArrayList<SearchSongEntity> entities) {
        this.mContext = context;
        this.mEntities = entities;
    }

    @Override
    public int getCount() {
        if (mEntities != null && mEntities.size() > 0)
            return mEntities.size();
        else
            return 0;
    }

    @Override
    public SearchSongEntity getItem(int position) {
        if (mEntities != null && mEntities.size() > 0)
            return mEntities.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHodler holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.search_listview_result_item, null);

            // 初始化
            holder = new ViewHodler();
            holder.search_listView_item_text = (TextView) convertView.findViewById(R.id.search_listView_item_text);

            convertView.setTag(holder);
        } else {
            holder = (ViewHodler) convertView.getTag();
        }
        SearchSongEntity songListResult = getItem(position);
        if (songListResult != null) {
            holder.search_listView_item_text.setText(songListResult.getSongname() + "-" + songListResult.getArtistname());
        }

        ListView searchListView = new ListView(mContext);

        searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        return convertView;
    }

    public void remove(SearchSongEntity searchSongEntity) {
        mEntities.remove(searchSongEntity);
        notifyDataSetChanged();
    }

    private class ViewHodler {
        private TextView search_listView_item_text;
    }
}
